package com.unitechs.trap.excel.webapi.service.impl.updata;

import com.unitechs.trap.common.pojo.trap.entity.DeleteAlarmtypeCode;
import com.unitechs.trap.excel.service.IUpdateTrapTypeOidService;
import com.unitechs.trap.excel.webapi.mapper.AllDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

@Service
@Slf4j
public class UpdateTrapTypeOidServiceImpl implements IUpdateTrapTypeOidService {

    @Autowired
    private AllDataMapper allDataMapper;

    /**
     * 将TrapOid替换为.开头
     */
    @Override
    @Transactional
    public void updateTrapOid() {
        List<String> listTrapOidFromTrapType = allDataMapper.listTrapOidFromTrapType();
        for (String trapTypeOid:listTrapOidFromTrapType){
            if (trapTypeOid.substring(0,1).equals(".")){
                continue;
            }
            String newTrapOid="."+trapTypeOid;
            String oldTrapOid=trapTypeOid;
            try {
                allDataMapper.updateTrapOidByTrapOid(newTrapOid, oldTrapOid);
            }catch (Exception e){
                String typeCode = allDataMapper.selectTypeCodeByTrapOid(oldTrapOid);
                String status = allDataMapper.selectStatus(oldTrapOid);
                allDataMapper.insertAlarmTypeCode(typeCode,status);
                allDataMapper.deleteTrapTypeByTrapOid(oldTrapOid);
            }

        }

    }
    @Override
    @Transactional
    public void updateVarBindTrapOid(){
        List<String> listTrapOidFromVarBind = allDataMapper.listTrapOidFromVarBind();
        for (String varBindOid:listTrapOidFromVarBind){
            if (varBindOid.substring(0,1).equals(".")){
                continue;
            }
            String newTrapOid="."+varBindOid;
            String oldTrapOid=varBindOid;
            try {
                allDataMapper.updateTrapOidByTrapOidFromVarBind(newTrapOid,oldTrapOid);
            }catch (Exception e){
                allDataMapper.deleteVarBindByTrapOid(oldTrapOid);
            }
        }

    }

    @Override
    public void deleteAlarmtypeByCode(){
        List<DeleteAlarmtypeCode> deleteAlarmtypeCodes = allDataMapper.listDeleteAlarmtypeCode();
        for (DeleteAlarmtypeCode deleteAlarmtypeCode:deleteAlarmtypeCodes){
            if (deleteAlarmtypeCode.getAlarmStatus().equals(0)){continue;}
            allDataMapper.deleteAlarmtypeByCode(deleteAlarmtypeCode.getAlarmtypeCode());
        }
    }

}
